
axios.defaults.baseURL = "http://localhost:8088";
//axios.defaults.timeout = 30 * 1000; // 30s

var rank = new Vue ({
    el: '#rank',
    data: {
        rankList: []
    },
    mounted(){
        this.getRankList()
    },
    methods: {
        getRankList : function() {
            let _this=this
            axios.get("/score/allScore")
                .then((res) => {
                    this.rankList = res.data.data
                })
                .catch((err) => {
                    let r=localStorage.getItem("rank")
                    console.log(r)
                    if(r!=null){
                        _this.rankList=JSON.parse(r)
                    }
                    console.log(err);
                });
        },
        
    }
})
var comment=new Vue({
    el: '#comment',
    data: {
        commentList: [],
        comment: ''
    },
    mounted(){
        this.getComments()
    }
    ,
    methods:{
        getTime:function(time){
            let a=new Date(time)
            return a.getFullYear()+'年'+a.getMonth()+'月'+a.getDate()+"  "+a.getHours()+":"+a.getMinutes()+":"+a.getSeconds()
        },
        getComments: function() {
            axios.get("/comment/allComment")
                .then((res) => {
                    this.commentList = res.data.data
                })
                .catch((err) => {
                    console.log(err);
                });
        },
        postComments: ()=> {
            let now=new Date()
            let id=gameStatus.user_id
            let userName=gameStatus.userName
            let cc=this.comment.comment
            let cl=this.comment.commentList
            if(!gameStatus.status){
                userName='匿名用户'
            }
            axios.post("/comment/commentUp",{
                    user_id:id,
                    user_name:userName,
                    comment:cc,
                    create_time:now
                })
                .then((res) => {
                    cl.push({
                        user_name:userName,
                        comment:cc,
                        create_time:now
                    })
                })
                .catch((err) => {
                    console.log(err);
                });
        },
    },
})


var gameStatus=new Vue({
    el:'#gameStatus',
    data:{
        coin:0,
        level:1,
        time:0,
        life:3,
        gameOver:false,
        status:false,
        user_id:0,
        userName:'匿名用户',
        userPwd:'',
        url:"http://www.xuyuxiang.space/favicon.ico",
        result:{
            coin:0,
            level:0,
            time:0
        }
    },
    mounted(){
        let _this=this

        if(sessionStorage.getItem("userName")){
            _this.userName=sessionStorage.getItem("userName")
        }
        if(sessionStorage.getItem("userPwd")){
            _this.userPwd=sessionStorage.getItem("userPwd")
        }

        setInterval(() => {
            _this.time++
        }, 1000);
    },
    methods:{
        updateFile:function(e){
            let self = this
            let file = e.target.files[0]
            let param = new FormData()  // 创建form对象
            param.append('file', file)  // 通过append向form对象添加数据
            param.append('chunk', '0') // 添加form表单中其他数据
            param.append('name',self.userName)
            //console.log(param.get('file')) // FormData私有类对象，访问不到，可以通过get判断值是否传进去
            let config = {
                headers: {'Content-Type': 'multipart/form-data'}
            }
            // 添加请求头
            axios.post('/user/file_upload', param, config)
            .then(res => {
                let tmp=res.data.msg
                tmp=tmp.replace(/\\/g,'/')
                console.log(tmp)
                self.url=tmp

                console.log(res.data)
            })
            .catch((err)=>{
                console.log(err)
            })
        },
        postScore : function(){
            let _this=this
            let userName=_this.userName
            if(!gameStatus.status){
                userName='匿名用户'
            }
            rank.rankList.push({
                user_name:userName,
                score:_this.result.coin,
                time:_this.result.time
            })
            rank.rankList.sort((a,b)=>{
                return b.score-a.score
            })
            localStorage.setItem("rank",JSON.stringify(rank.rankList))
            console.log(localStorage.rank)
            axios.post("/score/scoreUp",{
                user_name:this.userName,
                score:this.result.coin,
                time:this.result.time
            })
            .then((res) => {
                console.log(res)
                rank.getRankList()
            })
            .catch((err) => {
                console.log(err);
            });
        },
        login: function() {
            let _this=this
            axios.get("/user/login",{
                params:{
                    name:_this.userName,
                    password:_this.userPwd
                }
            })
            .then((res) => {
                console.log(res)
                if(res.data.code!=-1){
                    console.log(res.data)
                    this.status = true
                    sessionStorage.setItem("userName",_this.userName)
                    sessionStorage.setItem("userPwd",_this.userPwd)
                    let tmp=res.data.data.url
                    console.log(tmp)
                    tmp=tmp.replace(/\\/g,'/')
                    console.log(tmp)
                    _this.url=tmp
                    alert('登录成功')
                }
                else{
                    alert('登录失败')
                }
                
            })
            .catch((err) => {
                alert('登录失败')
                console.log(err);
            });
        },
        regist: function() {
            let _this=this
            console.log(_this.userName)
            axios.get("/user/register",{
                params:{
                    name:_this.userName,
                    password:_this.userPwd
                }
            })
            .then((res) => {
                console.log(res)
                console.log("!!!!")
                console.log(_this.userName)
                if(res.data.code!=-1){
                    
                    _this.status = true
                    sessionStorage.setItem("userName",_this.userName)
                    sessionStorage.setItem("userPwd",_this.userPwd)
                    alert('注册成功')
                }
                else{
                    alert('注册失败')
                }
                
            })
            .catch((err) => {
                alert('注册失败')
                console.log(err);
            });
        },
        gameInit:function(){
            this.result.coin=this.coin
            this.result.level=this.coin
            this.result.time=this.time
            this.coin=0;
            this.level=1;
            this.life=3;
            this.time=0;
            this.gameOver=true;
        },
        closeDia:function(){
            this.time=0;
            this.gameOver=false;
            this.postScore()
        }
    }
})

function load() {
    
    
}